﻿using System;
using System.Drawing;
using System.Windows.Forms;
using GMap.NET;
using LeakView.common;
using LeakView.service;
using LeakView.view;
using LeakView.view.controls;
using log4net;

namespace LeakView
{
    public partial class MainFrame : Form
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(MainFrame));

        private readonly Splash splash = new Splash();
        
        public MainFrame()
        {
            InitializeComponent();
        }

        private void showOnMonitor(int showOnMonitor)
        {
            Screen[] sc;
            sc = Screen.AllScreens;
            if (showOnMonitor >= sc.Length)
            {
                showOnMonitor = 0;
            }

            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top);
            // If you intend the form to be maximized, change it to normal then maximized.  
            this.WindowState = FormWindowState.Normal;
            this.WindowState = FormWindowState.Maximized;

            int miscRatio = AnyConfigs.get("APP").get("MiscRatio").GetOrDefault(27);
            miscRatio = Math.Min(100, miscRatio);
            miscRatio = Math.Max(0, miscRatio);

            float ratio = (float)(100 - miscRatio) / 100;
            this.splitContainer1.SplitterDistance = (int)(sc[showOnMonitor].Bounds.Width * ratio);

        }

        private void MainFrame_Load(object sender, EventArgs e)
        {
            if (!CSharpUtilities.IsDesignMode())
            {
                int screen = AnyConfigs.get("APP").get("Screen").GetOrDefault(0);
                showOnMonitor(screen);

                timerSplash.Enabled = true;

                applyUI();

                splash.Add(viewMap1);
                splash.Add(viewGraphic1);
                splash.Add(AlarmModal.me);
                splash.Add(SoundManager.me);
                splash.Add(this.viewAlarm1);
                splash.Add(NetworkStateManager.me);

                viewMap1.LeakComfirmAction = new Action<AjaxLeakInfo>(ConfirmLeak);
                viewGraphic1.LeakComfirmAction = new Action<AjaxLeakInfo>(ConfirmLeak);

            }

            labelUserTitle.Text = AnyConfigs.get("APP").get("UserTitle").GetOrDefault("");

        }

        private void applyUI()
        {
            this.splitContainer1.Panel2Collapsed = !Properties.Settings.Default.ui_misc;
            switch(Properties.Settings.Default.ui_view)
            {
                case 1:
                    this.splitContainer2.Panel1Collapsed = false;
                    this.splitContainer2.Panel2Collapsed = true;
                    break;
                case 2:
                    this.splitContainer2.Panel1Collapsed = true;
                    this.splitContainer2.Panel2Collapsed = false;
                    break;
                case 0:
                default:
                    this.splitContainer2.Panel1Collapsed = false;
                    this.splitContainer2.Panel2Collapsed = false;
                    break;
            }
        }

        private void buttonConfig_Click(object sender, EventArgs e)
        {
            /*
            FormTest test = new FormTest();
            test.ShowDialog();
            */

            FormConfig dlg = new FormConfig();
            DialogResult result = dlg.ShowDialog();
            if(result == DialogResult.OK)
            {
                applyUI();
            }
        }

        private void timerSplash_Tick(object sender, EventArgs e)
        {
            this.timerSplash.Enabled = false;

            try
            {

                // 显示时间
                this.label_time.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                // 处理数据
                if (MainModule.pair.Value.Valid)
                {
                    try
                    {
                        PointLatLng first = new PointLatLng(MainModule.pair.Value.Value.head.lat, MainModule.pair.Value.Value.head.lng);
                        PointLatLng last = new PointLatLng(MainModule.pair.Value.Value.tail.lat, MainModule.pair.Value.Value.tail.lng);
                        PipeModal.ME.Parse(first, last, MainModule.pair.Value.Value.gps);
                    }
                    catch (Exception ex)
                    {
                        log.Error("Exception on splash update mainmodule.pair", ex);
                    }

                }

                try
                {
                    this.splash.splash();
                }
                catch (Exception ex)
                {
                    log.Error("Exception on splashing.", ex);
                }

            }
            finally
            {
                this.timerSplash.Enabled = true;
            }

        }

        private void buttonHistory_Click(object sender, EventArgs e)
        {
            FormHistory dlg = new FormHistory();
            dlg.Show();
        }

        public void ConfirmLeak(AjaxLeakInfo leak)
        {
            FormConfirmLeak form = new FormConfirmLeak(leak);
            if(DialogResult.OK == form.ShowDialog())
            {
                if(form.IsConfirmAll)
                {
                    MainModule.ConfirmAll();
                }
                else
                {
                    MainModule.Confirm(leak);
                }
            }
        }

        private void label_title_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if(this.splitContainer3.Panel2Collapsed)
            {
                this.splitContainer3.Panel2Collapsed = false;
                this.viewDebug1.start();
            }
            else
            {
                this.splitContainer3.Panel2Collapsed = true;
                this.viewDebug1.stop();
            }
        }

        private void MainFrame_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.viewDebug1.stop();
        }

        private void label_server_Click(object sender, EventArgs e)
        {
            SoundManager.me.confirmAlarm();
        }

        private void buttonMore_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip.Show(this.buttonMore, new Point(this.buttonMore.Left, this.buttonMore.Bottom), ToolStripDropDownDirection.BelowLeft);
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            FormDataAlarmHistory dlg = new FormDataAlarmHistory();
            dlg.ShowDialog();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FormNetworkAlarmHistory dlg = new FormNetworkAlarmHistory();
            dlg.ShowDialog();
        }
    }





}
